package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.UserDetailDo;
import com.weijianhuawen.wjblog.common.domain.dos.UserDo;
import com.weijianhuawen.wjblog.common.domain.qo.UserDetailQo;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description: 用户登录对应mapper
 * @date: 2024-10-15 22:35
 */
public interface UserMapper extends BaseMapper<UserDo> {
    //根据用户名查询用户数据
    default UserDo findByUsername(String username) {
        LambdaQueryWrapper<UserDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDo::getUsername, username);
        return selectOne(wrapper);
    }
    //更新密码
    default int updatePasswordByUsername(String username, String password) {
        LambdaUpdateWrapper<UserDo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserDo::getPassword, password);
        wrapper.set(UserDo::getUpdateTime, LocalDateTime.now());
        //更新条件
        wrapper.eq(UserDo::getUsername, username);

        return update(null, wrapper);
    }

    // 分页获取注册用户账号
    default Page<UserDo> selectUserPageList(Long current, Long size, Long userId, String username) {
        // 创建分页
        Page<UserDo> page = new Page<>(current, size);

        // 创建查询语句
        LambdaQueryWrapper<UserDo> wrapper = Wrappers.<UserDo>lambdaQuery()
                .eq(Objects.nonNull(userId), UserDo::getId, userId)
                .like(StringUtils.isNotBlank(username), UserDo::getUsername, username)
                .orderByDesc(UserDo::getUpdateTime);

        // 进行分页查询
        return selectPage(page, wrapper);
    }

}
